package com.firstgroup.controller;

import com.alibaba.excel.EasyExcel;
import com.firstgroup.Utils.ResponseResult;
import com.firstgroup.domain.Comment;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.domain.QuessionBank;
import com.firstgroup.domain.User;
import com.firstgroup.repository.InterviewersRepository;
import com.firstgroup.repository.UserRepository;
import com.firstgroup.service.InterViewService;
import com.firstgroup.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 创作人 王箫凯
 * Date 2021/8/9
 * Time 9:41
 *
 * @author 王箫凯
 */
@Api(tags = "用户操作类")
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    InterViewService interViewService;
    @Resource
    UserRepository userRepository;
    @Resource
    InterviewersRepository interviewersRepository;

    @ApiOperation(value = "用户登录类")
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestParam("username") String username,
                                @RequestParam("password") String password) {
        System.out.println(username + "=======" + password);
        return userService.login(username, password);
    }

    @ApiOperation(("更改面试人员的状态"))
    @ApiImplicitParams({
            @ApiImplicitParam(name = "interviewId", value = "ID", required = true),
            @ApiImplicitParam(name = "state", value = "状态", required = true)
    })
    @PostMapping("/changeState")
    @ResponseBody
    public ResponseResult changeState(@RequestParam("interviewId") Integer interviewId, @RequestParam("state") String state) {
        //验证参数的合理性
        final String[] STATES = {"待面试", "已录取", "待一面", "待二面", "待三面", "已淘汰", "待笔试"};
        boolean flag = true;
        for (String theState :
                STATES) {
            if (state.equals(theState)) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return ResponseResult.error("错误的状态");
        }
        return userService.updateState(interviewId, state);
    }

    @GetMapping("/findAllInterviews")
    @ResponseBody
    public ResponseResult findAllInterviews() {
        List<Interviewers> list = interviewersRepository.findAll();

        return ResponseResult.success("查找成功", list);
    }

    @ApiOperation(value = "查看单个面试人员信息")
    @GetMapping("/findByStudentId")
    @ResponseBody
    public ResponseResult findByStudentId(Integer interviewId) {
        if (interviewId==null || interviewId<=0){
            return ResponseResult.error("数据异常");
        }
        Interviewers interviewers = userService.findInterviewersByInterviewId(interviewId);
        if (interviewers == null) {
            return ResponseResult.error("查找失败，该人员不存在");
        }
        return ResponseResult.success("查询成功", interviewers);
    }

    @ApiOperation(value = "生成excel 表格")
    @GetMapping
    @ResponseBody
    public String createExcel(String state, HttpServletResponse response) throws IOException {
        //判断state是否符合标准
        final String[] STATES = {"待面试", "已录取", "待一面", "待二面", "待三面", "已淘汰", "待笔试","所有人"};
        boolean flag = true;
        for (String isState : STATES) {
            if (isState.equals(state)) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return "不存在该状态";
        }
        //到达此处为正确的结果
        List<Interviewers> list ;
        if ("所有人".equals(state)) {
            //所有人查询：
            list=interviewersRepository.findAll();
        }else {
            list = interviewersRepository.findAllByState(state);
        }

        if (list.size() == 0) {
            return "当前无" + state + "的面试人员";
        }
        //生成excel表格
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = state + "名单.xlsx";
        fileName = URLEncoder.encode(fileName, "GB2312");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), Interviewers.class).sheet("工作区间").doWrite(list);



        return "成功";
    }

    @ApiOperation("设置面试人员的面试时间、地点")
    @ResponseBody
    @PostMapping("/setPlaceAndTime")

    public ResponseResult setPlaceAndTime(Integer[] interviewsIds, String time, String place) {
        String msg = "";
        boolean flag = false;
        for (Integer interviewsId : interviewsIds) {
            Interviewers interviewers = interviewersRepository.findInterviewersByInterviewersId(interviewsId);
            if (interviewers == null) {
                msg = "用户信息未找到";
                flag = true;
                break;
            }
        }
        if (flag) {
            return ResponseResult.error(msg);
        }
        return userService.setPlaceAndTime(place, time, interviewsIds);
    }

    @GetMapping("/findByName")
    @ResponseBody
    @ApiOperation("根据名字查询->可模糊")
    @ApiImplicitParam(name = "name", value = "姓名", required = true)
    public ResponseResult findByName(String name) {
        if (name.length() > 6) {
            return ResponseResult.error("请输入正确的名字");
        }
        return userService.findByName(name);
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    @ApiOperation(value = "修改用户功能")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "id",required = true,type = "int"),
            @ApiImplicitParam(value = "用户名字",name = "name",required = true,type = "String"),
            @ApiImplicitParam(value = "用户账号",name = "username",required = true,type = "String")

    })
    public User updateUser(int id, String name, String username){
        userService.updateUser(id, name, username);
        User user=new User();
        user.setUsername(username);
        user.setName(name);
        return user;

    }

    @ResponseBody
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "遍历面试官id和姓名")
    public List<Object> findIdAndName(){
     return userService.findIdAndName();

    }

    @ResponseBody
    @GetMapping("/FuzzyQuery")
    @ApiOperation(value = "进行模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "选择 0 为 名字的模糊查询，1为班级的模糊查询",name = "choose",required = true),
            @ApiImplicitParam(value = "需要进行模糊查询的内容",name = "like",required = true)
    })
    public ResponseResult fuzzyQuery(Integer choose, String like){
        if (choose==null){
            return ResponseResult.error("请进行您的选择");
        }
        System.out.println(like);
        like=like.replace(" ","");
        if (like==null || "".equals(like)){
            return ResponseResult.error("请输入您需要查询的内容");
        }
        return userService.FuzzyQuery(choose,"%"+like+"%");
    }
    @ResponseBody
    @PostMapping("/BatchEdit")
    public ResponseResult batchChangeState(Integer[] ids,String state){
        //判断state是否符合标准
        final String[] STATES = {"待面试", "已录取", "待一面", "待二面", "待三面", "已淘汰", "待笔试","所有人"};
        boolean flag = true;
        for (String isState : STATES) {
            if (isState.equals(state)) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return ResponseResult.error("不存在该状态");
        }
        if (ids.length==0){
            return ResponseResult.error("请选择需要被修改面试人员");
        }
        return userService.batchChange(ids,state);
    }
    @ApiOperation("查看所有拉入黑名单的学生")
    @GetMapping("/getBanUserList")
    @ResponseBody
    public ResponseResult findBanUsers(){
        return userService.findBanUsers();
    }
    @PostMapping("/addBanUser")
    @ResponseBody
    @ApiOperation("添加黑名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "操作人员的ID"),
            @ApiImplicitParam(name = "interviewId",value = "黑名单用户id"),
            @ApiImplicitParam(name = "content",value = "原因")
    })
    public ResponseResult addBanUser(int userId, Integer interviewId, String content) {
        //先判断是否存在该用户
        if (interviewId == null || interviewId <= 0) {
            return ResponseResult.error("数据异常");
        }
        return userService.addBanUser(userId,interviewId,content);
    }

    @ResponseBody
    @ApiOperation("根据interviewID进行查询被封禁的用户")
    @GetMapping("/getBanUserById")
    public ResponseResult getBanUserById(Integer integerViewId){
        if (integerViewId==null||integerViewId<=0){
            return ResponseResult.error("数据异常");
        }
        return userService.findBanUserByInterviewId(integerViewId);
    }
}